
import 'package:flutter/material.dart';
import 'package:flutter_module/page_a.dart';
import 'package:flutter_module/page_b.dart';
import 'package:flutter_module/page_home.dart';
import 'package:flutter_module/page_activitylifecycle.dart';

import ' channel/page_test_method_channel.dart';
import 'eventbus_mgr.dart';
import ' channel/methodchannel_mgr.dart';

void main(){
  runApp(MyApp());
  // testFuture();
}
/**
 把 MyApp 改成 StatefulWidget，
 同时创建一个 MethodChannel 用于接收原生 App 发送的消息，当启动 FlutterActivity 时，
 使用该 MethodChannel 将要进入的界面路由发送过来，然后将 DefaultHomePage 替换掉。
*/
class MyApp extends StatefulWidget {
  @override
  State createState(){
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  /// Flutter 页面入口
  Widget _initRoute = const DefaultHomePage();

  @override
  void initState() {
    super.initState();
    // debugger();
    registerEvent();
    MyMethodChannel.registerCallFromNative();
  }

  @override
  Widget build(BuildContext context) {
    // debugger();
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        platform: TargetPlatform.iOS,
      ),
      // home 设置成变量，当接收到 Android 发过来的路由时，
      // 将 home 修改以实现入口的切换
      home: _initRoute,
      routes: MYRouter.routes,    //路由map
      initialRoute: MYRouter.initialRoute,    //初始页面
      onGenerateRoute: MYRouter.generateRoute,    //路由拦截器
      // onUnknownRoute: MYRouter.unknownRoute,    //无匹配的默认路由
    );
  }

  void registerEvent(){
    eventBus.on<RouteEvent>().listen((event) {
      // All events are of type UserLoggedInEvent (or subtypes of it).
      print(event);
      // debugger();
      switch (event.arguments){
        case PageA.routeName:
          _initRoute = PageA();
          break;
        case PageB.routeName:
          _initRoute = PageB();
          break;
        case page_test_method_channel.routeName:
          _initRoute = page_test_method_channel();
          break;
        case ActivityLifecycleWatcher.routeName:
          _initRoute = ActivityLifecycleWatcher();
          break;
        case '/':
        default:
          _initRoute = const DefaultHomePage();
          break;
      }
      setState(() {});
    });
  }

}


class MYRouter {
  static final Map<String, WidgetBuilder> routes = {
    // 记得在页面里面 声明静态变量 static const String routeName = "/";
    PageA.routeName: (ctx) =>PageA(),
    //  声明静态变量 static const String routeName = "/about";
    PageB.routeName: (ctx) => PageB()
  };
  static final String initialRoute = DefaultHomePage.routeName;
  static final RouteFactory generateRoute = (settings) {
    if (settings.name == PageB.routeName) {
      return MaterialPageRoute(
          builder: (ctx) {
            // return PageB(settings.arguments);
            return PageB();
          }
      );
    }
    return null;
  };
  static final RouteFactory unknownRoute = (settings) {
    return MaterialPageRoute(
        builder: (ctx) {
          return const DefaultHomePage();
        }
    );
  };
}